<template>
  <view class="detail-container" :style="'height: calc(100vh - ' + barHeight * 2 + 'rpx)'">
    <!-- <scroll-view scroll-y :scroll-with-animation="true" :enable-back-to-top="true" refresher-background="#f7f8fa"> -->
    <view>
      <ve-card
        :title="'合同编号：' + (record.contractNo || '-')"
        :sub-title="record.contractStatusName"
        :type="
          record.contractStatus == 1 || record.contractStatus == -2
            ? 'gray'
            : record.contractStatus == 2
            ? 'blue'
            : record.contractStatus == 3 || record.contractStatus == 10
            ? 'green'
            : record.contractStatus == 4 ||
              record.contractStatus == 5 ||
              record.contractStatus == 6 ||
              record.contractStatus == 7 ||
              record.contractStatus == 8 ||
              record.contractStatus == 11 ||
              record.contractStatus == -1
            ? 'pink'
            : record.contractStatus == 9
            ? 'purple'
            : 'gray'
        "
        v-for="(record, index) in dataSource"
        :key="record.contractStatus"
      >
        <ve-cell label="创建节点">{{ record.createWay == '2' ? '-' : record.createNode || '-' }}</ve-cell>
        <ve-cell label="合同类型">{{ record.contractTypeName || '-' }}</ve-cell>
        <ve-cell label="归属门店">{{ record.siteName || '-' }}</ve-cell>
        <ve-cell label="发起人">{{ record.startPersonName || '-' }}</ve-cell>
        <ve-cell label="发起签署时间">{{ record.startTime || '-' }}</ve-cell>
        <ve-cell label="发起节点">{{ record.startNode || '-' }}</ve-cell>
        <ve-operate-list :operates="operates" :record="record" />
      </ve-card>
      <!-- 加载结束列表为空时展示 -->
      <ve-empty v-if="dataSource.length == 0"></ve-empty>
    </view>
    <!-- </scroll-view> -->
    <!-- 维护 -->
    <maintenanceContract ref="maintenances" @ok="maintenanceFn" />
  </view>
</template>

<script setup>
  import { usePermission } from '@/hooks/usePermission'
  import { ref } from 'vue'
  import { onShow } from '@dcloudio/uni-app'
  import { appConfig } from '@/config/index.js'
  import maintenanceContract from '@/pages/order/components/customer/maintenance-contract.vue'
  import { shortorderdetail } from '@/api/dict/detail'
  import { getOrdrDetailsAPI } from '@/api/order/lOrder'
  import { dzcontractAPI, czcontractAPI } from '@/api/order/contract'
  import { userStore } from '@/stores'
  console.log(appConfig.env)
  console.log(getApp().globalData.requestUrl)
  const barHeight = ref(0)
  const maintenances = ref(null)
  const orderCode = ref()
  const dataSource = ref([])
  const props = defineProps({
    orderId: [String, Number],
    orderType: {
      type: String,
      default: '2', //  2短租 3长租
    },
    // 合同付费类型
    contractPayType: {
      type: Number,
      default: 0,
    },
    permissionPrefix: String,
  })
  // 是否有对应按钮权限
  const hasPermission = (permissionSubfix) => {
    const permission = props.permissionPrefix + '.' + permissionSubfix
    return usePermission(permission)
  }

  // 完整权限字符串
  const getPermission = (permissionSubfix) => {
    return props.permissionPrefix + '.' + permissionSubfix
  }

  const handContractPermission = ref(false)
  const userInfoStore = userStore()
  userInfoStore.setEpDetail().then((res) => {
    let val = res.config
    console.log(usePermission('svmsapp:order:short-order.handContract'), '短租权限')
    console.log(usePermission('svmsapp:order:long-order.handContract'), '长租权限')
    console.log(val, '测试权限')
    if (val.userEcontractEnabled == 1) {
      if (val.userContractServiceType.includes(0)) {
        console.log('有短租权限')
        handContractPermission.value = hasPermission('handContract')
      } else if (val.userContractServiceType.includes(1)) {
        console.log('有长租权限')
        handContractPermission.value = hasPermission('handContract')
      }
    }
  })
  // 查询参数
  const queryParam = ref({})
  const loadData = () => {
    if (props.orderType == 2) {
      queryParam.value.payType = props.contractPayType
      dzcontractAPI(queryParam.value).then((res) => {
        dataSource.value = res.result
      })
    } else {
      queryParam.value.contractAttribute = props.contractPayType
      czcontractAPI(queryParam.value).then((res) => {
        dataSource.value = res.result
      })
    }
  }
  const resetRefresh = () => {
    loadData()
  }
  // 上传合同完成
  const maintenanceFn = () => {
    resetRefresh()
  }
  // 订单信息
  const orderInfo = ref({})
  onShow(() => {
    console.log('短租&长租：', props.orderType)
    if (props.orderId) {
      if (props.orderType == 2) {
        shortorderdetail({ id: props.orderId }).then((res) => {
          orderCode.value = res.result.orderInfo.orderCode
          queryParam.value = {
            orderId: props.orderId,
            orderCode: orderCode.value,
            orderType: 1,
            payType: 0,
          }
          if (orderCode.value) {
            loadData()
          }
          orderInfo.value = res.result.orderInfo
        })
      } else {
        queryParam.value = {
          orderId: props.orderId,
          orderType: 2,
          pageSize: 10,
          contractAttribute: 2,
        }
        loadData()
        getOrdrDetailsAPI({ id: props.orderId }).then((res) => {
          orderInfo.value = res.result.orderInfo
        })
      }
    }
    // 获取滑动高度
    uni.getSystemInfo({
      success: function (res) {
        console.log(res.statusBarHeight)
        barHeight.value = res.statusBarHeight
      },
    })
  })

  const operates = ref([
    {
      label: '编辑合同附件', //仅长租
      permission: getPermission('contractMaintenance'),
      show: (record) =>
        props.orderType == 3 && record.contractAttribute == 2 && (record.contractStatus == 10 || record.contractStatus == 11),
      method: (record) => {
        console.log('record', record)
        maintenances.value.open(record, props.orderId)
      },
    },
  ])
</script>

<style lang="scss" scoped>
  .detail-container {
    padding: 24rpx;
  }
</style>
